package com.nianzhen.recruitment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public abstract class BaseNetFragment extends BaseFragment {


    protected View mNoNetLayout;
    protected View mLoadingLayout;
    protected View mDataLayout;

    private Button mRetryBtn;

    protected abstract void loadData();

    protected void inflate(LayoutInflater inflater,int resId,ViewGroup container){
        super.inflate(inflater,resId,container);
        initNetLayout();
    }

    protected void initNetLayout(){
        if (mView != null){
            if (mRetryBtn == null){
                mRetryBtn = (Button) mView.findViewById(R.id.net_retry_btn);
                if(mRetryBtn != null){
                    mRetryBtn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            loadData();
                        }
                    });
                }
            }
            if (mNoNetLayout == null){
                mNoNetLayout = mView.findViewById(R.id.no_net_layout);
            }
            if (mLoadingLayout == null){
                mLoadingLayout = mView.findViewById(R.id.loading_layout);
            }
            if (mDataLayout == null){
                mDataLayout = mView.findViewById(R.id.content_layout);
            }
        }
    }

    protected void toLoadingLayout() {
        if (mNoNetLayout != null) {
            mNoNetLayout.setVisibility(View.GONE);
        }
        if (mLoadingLayout != null) {
            mLoadingLayout.setVisibility(View.VISIBLE);
        }
        if (mDataLayout != null) {
            mDataLayout.setVisibility(View.GONE);
        }
    }

    protected void toNoNetLayout() {
        if (mNoNetLayout != null) {
            mNoNetLayout.setVisibility(View.VISIBLE);
        }
        if (mLoadingLayout != null) {
            mLoadingLayout.setVisibility(View.GONE);
        }
        if (mDataLayout != null) {
            mDataLayout.setVisibility(View.GONE);
        }
    }

    protected void toDataLayout() {
        if (mNoNetLayout != null) {
            mNoNetLayout.setVisibility(View.GONE);
        }
        if (mLoadingLayout != null) {
            mLoadingLayout.setVisibility(View.GONE);
        }
        if (mDataLayout != null) {
            mDataLayout.setVisibility(View.VISIBLE);
        }
    }
}
